#include "seatshow.h"
#include "ui_seatshow.h"
#include "seatselect.h"
#include "QMouseEvent"
#include "reservehome.h"
#include"QGridLayout"
#include"QPainter"
#include"cmath"
#include"QDebug"
seatSelect *seatshow::s = nullptr;
seatshow::seatshow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::seatshow)
{
    ui->setupUi(this);
    s=new seatSelect;
    setFixedSize(700,600);
    setWindowFlags(Qt::FramelessWindowHint);
    //初始化座位按钮
    int c = reserveHome::index_campus;
    int f = reserveHome::index_floor;
    int a = reserveHome::index_area;
    area area = reserveHome::campuses[c].f[f].a[a];

        for (int i = 0; i < area.seatNum; ++i) {
            QPushButton* button = new QPushButton(area.s[i].name, this);
            button->setFixedSize(seatRadius*2, seatRadius);
            if (area.s[i].isUnbooked) {
                button->setStyleSheet("background-color: lightgreen; color: white;");
            } else {
                button->setStyleSheet("background-color: red; color: white;");
                button->setDisabled(true);
            }
            seatButtons.append(button);
            connect(button, &QPushButton::clicked, [=]() { onSeatClicked(i,button); });
        }
    update();
}

seatshow::~seatshow()
{
    delete ui;
}

void seatshow::paintEvent(QPaintEvent *event) {
    Q_UNUSED(event);
    if(seatButtons.size()==16){
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 计算桌子布局的偏移量，使桌子居中
    int tableSpacingX = width() / 5;
    int tableSpacingY = height() / 5;
    int xOffset = (width() - 2 * tableSpacingX) / 2;
    int yOffset = (height() - 2 * tableSpacingY) / 2;

    int buttonIndex = 0;
    for (int i = 0; i < 4; ++i) {
        // 计算桌子中心位置
        int tableX = xOffset + (i % 2) * 2 * tableSpacingX-20;
        int tableY = yOffset + (i / 2) * 2 * tableSpacingY+50;

        // 绘制圆桌
        painter.setBrush(Qt::gray);
        painter.drawEllipse(QPoint(tableX, tableY), tableRadius, tableRadius);

        for (int j = 0; j < 4; ++j) {
            if (buttonIndex >= seatButtons.size()) {
                break;
            }
            // 计算座位在圆桌边缘向中间靠拢一些的位置
            double angle = j * M_PI / 2;
            int seatX = tableX + static_cast<int>((tableRadius - seatRadius / 2) * std::cos(angle))-15;
            int seatY = tableY + static_cast<int>((tableRadius - seatRadius / 2) * std::sin(angle));
            QPushButton* button = seatButtons[buttonIndex];
            button->move(seatX - seatRadius / 2, seatY - seatRadius / 2);
            buttonIndex++;
        }
    }}
    else if(seatButtons.size()==20){
        QPainter painter(this);
                painter.setRenderHint(QPainter::Antialiasing);

                // 总宽度和总高度
                int totalWidth = width();
                int totalHeight = height();

                // 座位宽度和高度
                int seatWidth = totalWidth / 10;
                int seatHeight = totalHeight / 5;

                // 小垂直间距
                int smallVerticalGap = totalHeight / 30;
                // 大垂直间距
                int largeVerticalGap = totalHeight / 10;

                // 列间距
                int columnGap = totalWidth / 30;

                // 起始x和y坐标
                int startX = (totalWidth - (5 * seatWidth + 4 * columnGap)) / 2;
                int startY = (totalHeight - (4 * seatHeight + 2 * smallVerticalGap + largeVerticalGap)) / 2+150;

                int buttonIndex = 0;
                int y = startY;
                for (int row = 0; row < 4; ++row) {
                        if (row == 2) {
                            y += largeVerticalGap;
                        }
                        else y += smallVerticalGap;
                    y += 30;
                    for (int col = 0; col < 5; ++col) {
                        if (buttonIndex >= seatButtons.size()) {
                            break;
                        }
                        int x = startX + col * (seatWidth + columnGap);

                        QPushButton* button = seatButtons[buttonIndex];
                        button->setGeometry(x, y, seatWidth, seatHeight);
                        buttonIndex++;
                    }
                    if(row==1){
                        //画一条过道
                        int aisleY = y + seatHeight;
                                       int aisleWidth = totalWidth / 10;

                                       painter.fillRect(0, y+30, width(), largeVerticalGap, Qt::lightGray);
                    }
                }
           }
    else if(seatButtons.size()==24){
        QPainter painter(this);
                painter.setRenderHint(QPainter::Antialiasing);

                // 总宽度和总高度
                int totalWidth = width();
                int totalHeight = height();

                // 座位宽度和高度
                int seatWidth = totalWidth / 10;
                int seatHeight = totalHeight / 5;

                // 小垂直间距
                int smallVerticalGap = totalHeight / 30;
                // 大垂直间距
                int largeVerticalGap = totalHeight / 10;

                // 列间距
                int columnGap = totalWidth / 30;

                // 起始x和y坐标
                int startX = (totalWidth - (5 * seatWidth + 4 * columnGap)) / 2-50;
                int startY = (totalHeight - (4 * seatHeight + 2 * smallVerticalGap + largeVerticalGap)) / 2+150;

                int buttonIndex = 0;
                int y = startY;
                for (int row = 0; row < 4; ++row) {
                        if (row == 2) {
                            y += largeVerticalGap;
                        }
                        else y += smallVerticalGap;
                    y += 30;
                    for (int col = 0; col < 6; ++col) {
                        if (buttonIndex >= seatButtons.size()) {
                            break;
                        }
                        int x = startX + col * (seatWidth + columnGap);

                        QPushButton* button = seatButtons[buttonIndex];
                        button->setGeometry(x, y, seatWidth, seatHeight);
                        buttonIndex++;
                    }
                    if(row==1){
                        //画一条过道
                        int aisleY = y + seatHeight;
                                       int aisleWidth = totalWidth / 10;

                                       painter.fillRect(0, y+30, width(), largeVerticalGap, Qt::lightGray);
                    }
                }
           }
}
//重要
void seatshow::onSeatClicked(int index,QPushButton *button) {

     seatSelect::seatName=button->text();
     qDebug()<<seatSelect::seatName;
     s->handleSeatClose(index);
     this->close();
}
void seatshow::mousePressEvent(QMouseEvent *event) {
    if (event->button() == Qt::LeftButton) {
                initialMousePosition = event->globalPos(); // 记录鼠标按下时的全局位置
                isDragging = true; // 开始拖动
            }
    }

void seatshow::mouseMoveEvent(QMouseEvent *event){
    if (isDragging && event->buttons() & Qt::LeftButton) {
                QPoint delta = event->globalPos() - initialMousePosition; // 计算鼠标移动的距离
                move(x() + delta.x(), y() + delta.y()); // 根据鼠标移动的距离来移动窗口
                initialMousePosition = event->globalPos(); // 更新鼠标位置
            }
    }
void seatshow::mouseReleaseEvent(QMouseEvent *event) {
        if (event->button() == Qt::LeftButton) {
            isDragging = false; // 结束拖动
        }
    }
void seatshow::on_pushButton_clicked()
{
    this->close();
}
